import { ref } from 'vue';
import { getPurchaseRequestFormDetailApi } from '@/api/sj/purchaseManagement/applicationForm/index';

const useEditApplicationForm = () => {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;

    const editShow = ref(false);
    const editType = ref(1);
    const editObj = ref<any>();

    /** 打开编辑弹窗 */
    const handleEditClick = async (row: any) => {
        const { purchaseOrderCode, applyDate, specsDetail, number, auxiliaryUnit, imageUrl } = row;
        const data = await getPurchaseRequestFormDetail(row.id);
        let list = {
            purchaseOrderCode,
            applyDate,
            specsDetail,
            number,
            auxiliaryUnit,
            imageUrl,
            ...(data || {})
        };

        editObj.value = list;
        editType.value = 1;
        editShow.value = true;
    };

    /** 打开列表审批的弹窗 */
    const handleApprovalClick = async (row: any) => {
        const { approvalStatus, purchaseOrderCode, applyDate, specsDetail, number, auxiliaryUnit, imageUrl } = row;
        if (approvalStatus != 1) return proxy?.$modal.msgError('请先编辑采购申请单!');
        const data = await getPurchaseRequestFormDetail(row.id);
        let list = {
            purchaseOrderCode,
            applyDate,
            specsDetail,
            number,
            auxiliaryUnit,
            imageUrl,
            ...(data || {})
        };

        editObj.value = list;
        editType.value = 2;
        editShow.value = true;
    };

    /** 关闭编辑弹窗 */
    const editClose = () => {
        editShow.value = false;
    };

    /** 获取详情 */
    const getPurchaseRequestFormDetail = async (id: any) => {
        return new Promise(async (resolve, reject) => {
            const { code, data } = await getPurchaseRequestFormDetailApi(id);
            if (code == 200) {
                resolve(data)
            }
        });
    };


    return {
        editShow,
        editType,
        editObj,
        handleApprovalClick,
        handleEditClick,
        editClose,
    }
};

export default useEditApplicationForm;